package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.AccountDao;
import com.edu.modules.baseinfo.dao.LessonDao;
import com.edu.modules.baseinfo.model.Lesson;
import com.edu.modules.baseinfo.model.LessonDetail;
import com.edu.modules.baseinfo.vo.LessonVideoListVo;
import com.edu.modules.baseinfo.vo.MyLessonListVo;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.comm.WXData;
import com.edu.modules.entity.TLessonEntity;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import com.sari.common.utils.IdGen;
import com.sari.common.utils.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.json.JsonObject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class LessonService extends BaseService {

	@Autowired
	private LessonDao lessonDao;

	@Autowired
	private AccountDao accountDao;

	public ResultVo saveLesson(JSONObject param){

		String week = param.getString("week");
		String weeks[] = week.split(",");

		TLessonEntity lesson = new TLessonEntity();
		param.put("lessonId", IdGen.uuid());
		param.put("createTime", DateUtils.getDateTime());
		param.put("signNumber",0);
		param.put("week",week);
		Reflections.initBean(lesson,param);

		lessonDao.saveObject(lesson);
		lessonDao.saveWeeks(IdGen.uuid(),weeks,DateUtils.getDateTime());

		return new ResultVo(true,"保存成功");
	}

	public List<Lesson> getLessonList(String lessonName,String lessonClass){

		String nowDate = DateUtils.getDate();
		return lessonDao.getLessonList(lessonName,lessonClass,nowDate);
	}

	public LessonDetail getLessonDetail(String lessonId){

		return lessonDao.getLessonDetail(lessonId);
	}


	public LessonVideoListVo getLessonRecordListByCode(String code){

		Map student = accountDao.getStudentId(WXData.getOpenId(code));
		String studentId = student.get("student_id").toString();//String.valueOf(accountDao.getStudentId(WXData.getOpenId(code)));
		if(studentId==null){
			return new LessonVideoListVo(null,"未注册");
		}else{
			return new LessonVideoListVo(lessonDao.getLessonVideoList(studentId),"success");
		}
	}

	public LessonVideoListVo getLessonVideoList(String studentId){

		return new LessonVideoListVo(lessonDao.getLessonVideoList(studentId),"success");
	}

	public MyLessonListVo getMyLessonList(String studentId){

		return new MyLessonListVo(lessonDao.getMyLessonList(studentId),"success");

	}

	public JSONObject getLessonGroup(){

		List lessonGroup = lessonDao.getLessonGroup();
		JSONObject result= new JSONObject();
		String groupStr = "";
		for(int i=0,len=lessonGroup.size(); i<len; i++){
			Map lesson_temp = (Map)lessonGroup.get(i);
			String name = lesson_temp.get("lesson_name").toString();
			groupStr += "<option value=\"" + name +"\">"+name+"</option>";
		}
		result.put("lessonGroup", groupStr);
		return result;
	}

	public JSONObject getTeacherLessonGroup(JSONObject param){

		String teacherId = param.getString("teacherId");
		List lessonGroup = lessonDao.getTeacherLessonGroup(teacherId);
		List classList = lessonDao.getTeacherClass(teacherId);
		JSONObject result= new JSONObject();
		String groupStr = "";
		for(int i=0,len=lessonGroup.size(); i<len; i++){
			Map lesson_temp = (Map)lessonGroup.get(i);
			String name = lesson_temp.get("subject_type").toString();
			groupStr += "<option value=\"" + name +"\">"+name+"</option>";
		}
		result.put("teacherLessonGroup", groupStr);
		result.put("classList",classList);
		return result;
	}

    public JSONObject getLessonByPage(JSONObject param) {

        int offset = param.getInteger("offset");
        int limit = param.getInteger("limit");
        String lessonName = param.getString("lessonName");
        String lessonClass = param.getString("lessonClass");
        String startTime = param.getString("startTime");
		String endTime = param.getString("endTime");

        List list = new ArrayList();
        list = lessonDao.getLessonByPage(lessonName,lessonClass,startTime,endTime,offset,limit);

        JSONObject result= new JSONObject();
        result.put("total",lessonDao.getLessonCounts(lessonName,lessonClass,startTime,endTime));
        result.put("rows", list);

        return result;
    }

	public JSONObject getLessonWeeks(String lessonId){

		JSONObject result= new JSONObject();
		result.put("weeks", lessonDao.getLessonWeeks(lessonId));
		return result;
	}

    public void updateLesson(JSONObject param){

        TLessonEntity objInfo = (TLessonEntity)lessonDao.loadObject(
        		TLessonEntity.class,param.getString("lessonId"));
		param.put("signNumber",param.getInteger("signNumber"));
		param.put("createTime",DateUtils.getDateTime());
        Reflections.initBean(objInfo,param);
        lessonDao.updateObject(objInfo);
		String weeks[] = param.getString("week").split(",");
		lessonDao.updateLessonWeeks(weeks,param.getString("lessonId"));
    }

    public ResultVo deleteLesson(String lessonId){

        lessonDao.deleteObjects("t_lesson","lesson_id",lessonId);
        return new ResultVo(true,"success");
    }

	public JSONObject getLessonCalendar(String studentId,String dateTime){

		List list = new ArrayList();
		List weekList = lessonDao.getLessonCalendar(studentId,dateTime);
		List<String> wList = new ArrayList();
		for(Object obj: weekList){

			Map m = (Map)obj;
			String weekStr = m.get("week").toString();
			String startDate = m.get("start_date").toString();
			String endDate = m.get("end_date").toString();
			if(weekStr.length()>1){
				String [] weeks = weekStr.split(",");
				for(String i:weeks){
					wList.add(i+","+startDate+","+endDate);
				}
			}else{
				wList.add(weekStr+","+startDate+","+endDate);
				//dList.add(startDate);
			}
		}
		for(String week:wList){

			List dateList = DateUtils.getDatesByWeek(week,
					DateUtils.getMonthByDate(dateTime));
			for(Object d: dateList){
				list.add(d);
			}
		}

		JSONObject result= new JSONObject();
		result.put("dateList",list);
		return result;
	}

	public JSONObject getMyLesson(String studentId,String dateTime){

		JSONObject result= new JSONObject();
		result.put("myLessonList",lessonDao.getMyLesson(studentId,dateTime,DateUtils.getDayForWeek(dateTime)));
		return result;
	}
}
